<?php
require_once (dirname(__FILE__).'/include/common.inc.php');


# 页面级别
# 0 = 所有人都可以访问
$conf_web_page_level = 0; 

class myPage extends page{
	private $hsellId = 0;
	private $prodId = 0;
	private $cateId = 0;
	function __construct(){
		parent::__construct();
		if(isset($_GET['id'])){
			$this->hsellId = $_GET['id'];
		}
		if(isset($_GET['pid'])){
			$this->prodId = $_GET['pid'];
		}
	}
	
	function getCateById(){
		if($this->prodId == 0){
			return null;
		}
		$row; 
		try{
			$sql = "
				SELECT cs2_cate.name,cs2_cate.cateId
				FROM cs2_prod LEFT JOIN cs2_cate ON (cs2_cate.cateId = cs2_prod.cateId)
				 WHERE (prodId=".$this->prodId.")";
			$row = $this->database->get_one($sql);
			$this->cateId = $row['cateId'];
		}catch (Exception $e) {
			$row = null;
		}
		return $row;
	}
	function getSameCateProd(){
		$lv;
		try{
			$sql = "SELECT hsellId,cs2_hsell.prodId, cs2_prod.name, cs2_prod.model, cs2_hsell.title,
					cs2_prod.info,cs2_prod.barcode,cs2_prod.size_l,cs2_prod.size_w,
					cs2_prod.size_h,weight,cs2_prod.cateId,icon,iconinfo,num,contents,price,sprice,
				IFNULL((SELECT cs2_image.fileId FROM cs2_image 
					WHERE cs2_image.prodId=cs2_prod.prodId AND cs2_image.defId=0 LIMIT 0,1),'no_image') AS fileId
				
				FROM cs2_hsell LEFT JOIN cs2_prod ON (cs2_prod.prodId = cs2_hsell.prodId)
			";
			$sql = $sql." WHERE (cateId=".$this->cateId.")";
			$sql = $sql." ORDER BY cateId;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			$lv = null;
		}
		return $lv;
	}
	
	function getProdById(){
		if($this->hsellId == 0){
			return null;
		}
		$row; 
		try{
			$sql = "
				SELECT cs2_hsell.hsellId,cs2_hsell.prodId, cs2_prod.name, cs2_prod.model, cs2_hsell.title,
					cs2_prod.info,cs2_prod.barcode,cs2_prod.size_l,cs2_prod.size_w,
					cs2_prod.size_h,weight,cs2_prod.cateId,icon,iconinfo,num,contents,price,sprice
				FROM cs2_hsell LEFT JOIN cs2_prod ON (cs2_prod.prodId = cs2_hsell.prodId)";
			$sql = $sql." WHERE (hsellId=".$this->hsellId.")";
			
			$row = $this->database->get_one($sql);
		}catch (Exception $e) {
			$row = null;
		}
		return $row;
	}
	
	function getImageList(){
		if($this->prodId == 0){
			return null;
		}
		$lv; 
		try{
			$sql = "SELECT fileId,defId FROM cs2_image WHERE prodId=".$this->prodId.";";
			
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			$lv = null;
		}
		return $lv;
	}
	# SELECT cs2_image.fileId FROM cs2_image WHERE cs2_image.prodId=cs2_prod.prodId AND cs2_image.defId=0 LIMIT 0,1),'no_image'
	function ajax_callback(){
		parent::ajax_callback();
		return json_encode($this->ajax_json);
	}
}

$pg = new myPage();
# 载入页面
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	# getCateById function is first.
	$config->smarty->assign('getCateById', $pg->getCateById());
	$config->smarty->assign('getSameCateProd', $pg->getSameCateProd());
	$config->smarty->assign('getImageList', $pg->getImageList());
	$config->smarty->assign('getProdById', $pg->getProdById());
	# 加载页面
	$pg->render($config->temp_page_path);
}else{
	print_r($pg->ajax_callback());
}


?>